package com.wjh.service.impl;

import com.wjh.constants.SystemConstants;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.MenuMapper;
import com.wjh.mapper.RoleMapper;
import com.wjh.pojo.LoginUser;
import com.wjh.pojo.User;
import com.wjh.service.*;
import com.wjh.utils.*;
import com.wjh.vo.AdminUserInfoVo;
import com.wjh.vo.UserInfoVo;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private RedisCache redisCache;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;

    @Resource
    private TagService tagService;

    @Resource
    private UserService userService;

    @Override
    public ResponseResult login(User user) {
                UsernamePasswordAuthenticationToken authenticationToken =
                        new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassWord());
        //来到我们自定义的UserDetailsServiceImpl类
        Authentication authenticate =
                authenticationManager.authenticate(authenticationToken);
        //判断是否通过认证
        if(Objects.isNull(authenticate)){
            //用户名或密码错误，抛出异常
            throw new RuntimeException("用户名或密码错误");
        }
        //认证通过，获取userId，生成Token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        Integer id = loginUser.getUser().getId();
        String jwt = JwtUtil.createJWT(id.toString());
        //把用户信息存入Redis
        redisCache.setCacheObject(SystemConstants.SYSTEM_USER_ID+id,loginUser);
        //把token返回
        Map<String,String> map = new HashMap<>();
        map.put("token",jwt);
        return ResponseResult.okResult(map);
    }


    @Override
    public ResponseResult logout() {
        //获取用户id
        Integer id = SecurityUtils.getUserId();
        if(id == null){
            throw new SystemException(AppHttpCodeEnum.PLEASE_AGAIN_LOGIN);
        }
        //从redis中删除用户信息
        redisCache.deleteObject(SystemConstants.SYSTEM_USER_ID+id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getInfo() {
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据用户Id查询权限信息
        List<String> perms = menuService.selectPermsByUserId(loginUser.getUser().getId());
        //根据用户Id查询角色信息
        List<String> roles = roleService.selectRolesById(loginUser.getUser().getId());
//        //获取用户信息
        User user = userService.getById(loginUser.getUser().getId());
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);


        //封装返回
        return ResponseResult.okResult(new AdminUserInfoVo(perms,roles,userInfoVo));
    }
}

